<h2>GRASS Minor cost path module (using r.walk)</h2>
<p>Find shortest path between 2 points using slope factor as cost value.</p>

<p>Script Licence is GNU/GPL</p>
<hr>
<pre><code>
"""
Description of your process which can be seen with pydoc.
"""
# Author:       Luca Casagrande
#               http://www.ominiverdi.org
# Lince:
#
# Your Process Description
# Copyright (C) 2006 Your Name
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

# PREREQUISITES
# 1) PyWPS: http://pywps.wald.intevation.org
# 2) GRASS  6.2
#
# INSTALLING INTO PYWPS
# following pywps instructions to put this short program into pywps/processes/
# adding 'minorcostpath.py' into pywps/process/__init__.py
#
#
import os,time,string,sys
class Process:
        def __init__(self):
                self.Identifier = "minorcostpath"
                self.processVersion = "0.1"
                self.storeSupport = "true"
                self.statusSupported = "false"
                self.Title="Shortest path"
                self.Abstract="Find the shortest path between 2 points using slope factor as cost"
                self.grassLocation="/home/doktoreas/data/grass/spearfish60/"
                self.Inputs = [
                    # 0
                    {
                        'Identifier': 'x1',
                        'Title': 'Start x coordinate',
                        'LiteralValue': {                                             
                        },
                        'dataType': type(0.0),
                    },
                    # 1
                     {
                        'Identifier': 'y1',
                        'Title': 'Start y coordinate',
                        'LiteralValue': {
                            'AnyValue':None,
                        },
                        'dataType': type(0.0),
                    },
                    # 2
                     {
                        'Identifier': 'x2',
                        'Title': 'End x coordinate',
                        'LiteralValue': {
                            'AnyValue':None,
                        },
                        'dataType': type(0.0),
                    },
                    # 3
                     {  
                        'Identifier': 'y2',
                        'Title': 'End y coordinate',
                        'LiteralValue': {
                            'AnyValue':None,
                        },
                        'dataType': type(0.0),
                    },

                        ]
                self.Outputs = [
                        {
                        'Identifier': 'output',
                        'Title': 'Resulting output map',
                        'ComplexValueReference': {
                        'Formats':["text/xml"],
                        }
                        },
                ]
        def execute(self):
             os.system("g.region -d")
             os.system("r.walk -k elevation=elevation.dem friction=slope output=output_walk coordinate=%s,%s stop_coordinate=%s,%s lambda=2.35 max_cost=0 percent_memory=100 nseg=4 walk_coef$
                             (self.Inputs[0]['value'],self.Inputs[1]['value'],self.Inputs[2]['value'],self.Inputs[3]['value']))
             os.system("r.drain input=output_walk output=pat_cost coordinate=%s,%s,%s,%s 1>&2" %\
                             (self.Inputs[0]['value'],self.Inputs[1]['value'],self.Inputs[2]['value'],self.Inputs[3]['value']))
             os.system("r.to.vect -s input=pat_cost output=path feature=line 1>&2")
             os.system("v.out.ogr format=GML input=path dsn=out.xml  olayer=path.xml 1>&2")

             if "out.xml" in os.listdir(os.curdir):
                        self.Outputs[0]['value'] = "out.xml"
                        return
             else:
                        return "Output file not created"


	
</code></pre>